<?php
class Tourismgoodscategoryadditive extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('tourism_goods_category_additive_model', 'tourism_goods_category_additive');
        $this->load->model('tourism_goods_category_model', 'tourism_goods_category');
    }

    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        $page_num = 20;
        $num = ($pg - 1) * 20;
        $config['first_url'] = base_url('tourismgoodscategoryadditive/grid').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismgoodscategoryadditive/grid');
        $config['total_rows'] = $this->tourism_goods_category_additive->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['result'] = $this->tourism_goods_category_additive->page_list($page_num, $num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $this->load->view('tourismgoodscategoryadditive/grid', $data);
    }

    public function add($category_id = 0)
    {
        if ($category_id == 0) {
            $this->error('tourismgoodscategory/grid', '', '参数有误');
        }
        $data['category_id'] = $category_id;
        $data['cate'] = $this->tourism_goods_category->getCategoryByLastId($category_id);
        $this->load->view('tourismgoodscategoryadditive/add', $data);
    }
    
    public function addPost()
    {
        $catagoryId = $this->input->post('category_id');
        $result = $this->tourism_goods_category_additive->insertBatchAdditive($this->input->post());
        if ($result) {
            $this->success('tourismgoodscategoryadditive/grid', array('category_id'=>$catagoryId), '添加成功');
        } else {
            $this->error('tourismgoodscategoryadditive/add', $catagoryId, '添加失败');
        }
    }
    
    public function edit($id = 0)
    {
        $result = $this->tourism_goods_category_additive->getAdditiveById($id);
        if ($result->num_rows() <= 0) {
            $this->error('tourismgoodscategory/grid', '', '属性不存在');
        }
        $additive = $result->row();
        $data['additive'] = $additive;
        $data['cate'] = $this->tourism_goods_category->getCategoryByLastId($additive->category_id);
        $this->load->view('tourismgoodscategoryadditive/edit', $data);
    }
    
    public function editPost()
    {
        $catagoryId = $this->input->post('category_id');
        $update = $this->tourism_goods_category_additive->updateTourismAdditive($this->input->post());
        if ($update) {
            $this->success('tourismgoodscategoryadditive/grid', array('category_id'=>$catagoryId), '修改成功');
        } else {
            $this->error('tourismgoodscategoryadditive/edit', array('category_id'=>$catagoryId), '修改失败');
        }
    }
    
    public function delete($id)
    {
        $result = $this->tourism_goods_category_additive->getAdditiveById($id);
        if ($result->num_rows() <= 0) {
            $this->error('tourismgoodscategoryadditive/grid', array('category_id'=>$this->input->post('category_id')), '删除失败');
        }
        $additive = $result->row();
        $category_id = $additive->category_id ? $additive->category_id : 0;
        if ($this->tourism_goods_category_additive->delete($id)) {
            $this->success('tourismgoodscategoryadditive/grid', array('category_id'=>$category_id), '删除成功');
        } else {
            $this->error('tourismgoodscategoryadditive/grid', array('category_id'=>$category_id), '删除失败');
        }
    }
}